package com.bj.jjz;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;

import com.bj.jjz.adapter.MyViewPagerAdapter;
import com.bj.jjz.fragments.FindFragment;
import com.bj.jjz.fragments.HomeFragment;
import com.bj.jjz.fragments.SettingFragment;
import com.luseen.luseenbottomnavigation.BottomNavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author Hu
 * @Time 2017/4/14 -- 10:50.
 * @Email 87683202@qq.com
 */
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {

    int[] image, color;
    @BindView(R.id.my_vp)
    ViewPager myVp;
    @BindView(R.id.bottomNavigation)
    BottomNavigationView bottomNavigation;
    @BindView(R.id.container)
    LinearLayout container;
    MyViewPagerAdapter myViewPagerAdapter;
    List<Fragment> fragments;
    HomeFragment homeFragment;
    FindFragment findFragment;
    SettingFragment settingFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
        initEvent();

    }

    private void initEvent() {
        myVp.addOnPageChangeListener(this);
    }

    private void init() {
        initFragment();
        initBottom();

    }

    private void initFragment() {
        fragments=new ArrayList<>();
        homeFragment=new HomeFragment();
        findFragment=new FindFragment();
        settingFragment=new SettingFragment();
        fragments.add(homeFragment);
        fragments.add(findFragment);
        fragments.add(settingFragment);
    }

    private void initBottom() {
        image = new int[]{R.drawable.ic_home_black_24dp,
                R.drawable.ic_dashboard_black_24dp,
                R.drawable.ic_notifications_black_24dp};


        color = new int[]{ContextCompat.getColor(this, R.color.colorPrimary), ContextCompat.getColor(this, R.color.colorPrimary),
                ContextCompat.getColor(this, R.color.colorPrimary)};

        myViewPagerAdapter = new MyViewPagerAdapter(this,getSupportFragmentManager(), fragments);
        myVp.setAdapter(myViewPagerAdapter);
        myVp.setOffscreenPageLimit(1);
        bottomNavigation.setUpWithViewPager(myVp,color,image);
        bottomNavigation.isWithText(true);
        bottomNavigation.isColoredBackground(false);
        bottomNavigation.disableViewPagerSlide();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigation.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
